package net.bplaced.streetfighter.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import net.bplaced.streetfighter.lib.model.Comment;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommentListAdapter
        extends BaseAdapter {

    private final static int DIVIDER_COLOR = Color.argb(255, 180, 180, 180);

    private final static LayoutParams LAYOUT_WIDTH_FILL_PARENT_HEIGHT_WRAP_CONTENT = new AbsListView.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    private final static LayoutParams LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT = new AbsListView.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    private final List<Comment> _comments;

    private final Context _context;

    public CommentListAdapter(Context context) {

        this._context = context;
        this._comments = new ArrayList<Comment>();
    }

    public List<Comment> getAll() {

        return this._comments;
    }

    public void add(Comment c) {

        this._comments.add(c);
        this.notifyDataSetChanged();
    }

    public void addAll(List<Comment> comments) {

        for (final Comment comment : comments) {
            this._comments.add(comment);
        }
        this.notifyDataSetChanged();
    }

    public void addAll(Comment[] comments) {

        for (final Comment comment : comments) {
            this._comments.add(comment);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return this._comments.size();
    }

    @Override
    public Comment getItem(int commentPosition) {

        return this._comments.get(commentPosition);
    }

    @Override
    public long getItemId(int commentPosition) {

        return this.getItem(commentPosition).id;
    }

    @Override
    public View getView(int commentIndex, View convertView, ViewGroup parent) {

        final Comment comment = this.getItem(commentIndex);

        final LinearLayout frame = new LinearLayout(this._context);
        frame.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_FILL_PARENT_HEIGHT_WRAP_CONTENT);
        frame.setOrientation(1);
        frame.setPadding(0, 2, 0, 2);

        final TextView authorNameView = new TextView(this._context);
        authorNameView.setText(comment.author);
        authorNameView.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
        frame.addView(authorNameView);

        final TextView dateTimeView = new TextView(this._context);
        try {
            dateTimeView.setText(new SimpleDateFormat("dd.MM.yyyy - HH:mm").format(new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss").parse(comment.commentDateTimeString)));
        } catch (final ParseException e) {
            e.printStackTrace();
        }
        dateTimeView.setTextSize(10);
        dateTimeView.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
        frame.addView(dateTimeView);

        final TextView commentTextView = new TextView(this._context);
        commentTextView.setText(comment.commentText);
        commentTextView.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
        commentTextView.setTextSize(13);
        commentTextView.setTag(comment);
        frame.addView(commentTextView);

        ((Activity) this._context).registerForContextMenu(commentTextView);

        if (comment.childComments.length > 0) {
            frame.addView(this.createChildCommentTree(comment.childComments, 15));
        }

        return frame;
    }

    /**
     * Create a visual tree with left-padded layouts for child comments.
     * 
     * @param childs
     *            Child comments of a comment
     * @param padding
     *            Value for left padding
     * @return Visual tree for child comments
     */
    private LinearLayout createChildCommentTree(Comment[] childs, int padding) {

        final LinearLayout childContainer = new LinearLayout(this._context);
        childContainer.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_FILL_PARENT_HEIGHT_WRAP_CONTENT);
        childContainer.setPadding(padding, 0, 0, 0);
        childContainer.setOrientation(1);

        for (final Comment comment : childs) {

            final LinearLayout frame = new LinearLayout(this._context);
            frame.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_FILL_PARENT_HEIGHT_WRAP_CONTENT);
            frame.setOrientation(1);
            frame.setPadding(0, 2, 0, 2);

            final View borderLine = new View(this._context);
            borderLine.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 1));
            borderLine.setBackgroundColor(CommentListAdapter.DIVIDER_COLOR);
            frame.addView(borderLine);

            final TextView authorNameView = new TextView(this._context);
            authorNameView.setText(comment.author);
            authorNameView.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
            frame.addView(authorNameView);

            final TextView dateTimeView = new TextView(this._context);
            try {
                dateTimeView.setText(new SimpleDateFormat("dd.MM.yyyy - HH:mm").format(new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss").parse(comment.commentDateTimeString)));
            } catch (final ParseException e) {
                e.printStackTrace();
            }
            dateTimeView.setTextSize(10);
            dateTimeView.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
            frame.addView(dateTimeView);

            final TextView commentTextView = new TextView(this._context);
            commentTextView.setText(comment.commentText);
            commentTextView.setLayoutParams(CommentListAdapter.LAYOUT_WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
            commentTextView.setTextSize(13);
            commentTextView.setTag(comment);
            frame.addView(commentTextView);

            ((Activity) this._context).registerForContextMenu(commentTextView);

            if (comment.childComments.length > 0) {
                frame.addView(this.createChildCommentTree(comment.childComments, (padding + 15)));
            } else {

            }

            childContainer.addView(frame);
        }

        return childContainer;
    }
}